package me.keiwu.pub.data;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * Created by kei on 23/05/2017.
 */
@Data
public class ResponseV2<T> {

    private int code;

    private String msg;


    private T data;

    private ResponseV2(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static ResponseV2 ok() {
        return getResponse(HttpStatus.OK.value(), "", null);
    }

    public static ResponseV2 ok(String msg) {
        return getResponse(HttpStatus.OK.value(), msg, null);
    }

    public static <T> ResponseV2 ok(T data) {
        return getResponse(HttpStatus.OK.value(), "", data);
    }

    public static ResponseV2 ok(int code, String msg) {
        return getResponse(code, msg, null);
    }

    public static <T> ResponseV2 ok(int code, T data) {
        return getResponse(code, "", data);
    }

    public static <T> ResponseV2 ok(int code, String msg, T data) {
        return getResponse(code, msg, data);
    }





    public static ResponseV2 fail() {
        return getResponse(HttpStatus.BAD_REQUEST.value(), "", null);
    }

    public static ResponseV2 fail(String msg) {
        return getResponse(HttpStatus.BAD_REQUEST.value(), msg, null);
    }

    public static <T> ResponseV2 fail(T data) {
        return getResponse(HttpStatus.BAD_REQUEST.value(), "", data);
    }

    public static ResponseV2 fail(int code, String msg) {
        return getResponse(code, msg, null);
    }

    public static <T> ResponseV2 fail(int code, T data) {
        return getResponse(code, "", data);
    }

    public static <T> ResponseV2 fail(int code, String msg, T data) {
        return getResponse(code, msg, data);
    }



    private static <T> ResponseV2 getResponse(int code, String msg, T data) {
        return new ResponseV2<>(code, msg, data);
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
